#include "../Server.hpp"

class EchoServer
{
private:
    void OnConnected(const Connection::connection_ptr& connetion)
    {
        DEBUG_LOG("new connection:%p", connetion.get());
    }

    void OnClosed(const Connection::connection_ptr& connetion)
    {
        DEBUG_LOG("close connection:%p", connetion.get());
    }

    void OnMessage(const Connection::connection_ptr& connection, Buffer* buf)
    {
        std::string clientmessage;
        buf->Read(&clientmessage, buf->GetReadableSize());
        DEBUG_LOG("%s", clientmessage.c_str());
        std::string str = "Hello Client!";
        connection->Send(str.c_str(), str.length());
        connection->ShutDown();
    }
public:
    EchoServer(int port) : _server(port)
    {
        _server.SetThreadCount(3);
        _server.EnableInactiveRelease(10);
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
    }
    void Start()
    {
        _server.Start();
    }
private:
    TcpServer _server;
};